/**
 *
 */
package com.dkm.dkmproject.utils;

import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import sun.net.www.content.image.png;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 文件工具类
 * @author liandyao
 * @date 2017年10月12日 下午3:28:42
 * @version 1.0
 */
@Component
public class FileTools {

	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

	@Resource
	private Config config ;
	/**
	 * 需要保存的文件地址和文件
	 * @param path 传入的地址名称,例如,会员模块,传入的地址是member,也可以是member/2017/这样的多重文件夹
	 * @param file
	 * @return  地址字符串,注意,如果返回的是空,则说明没有选择上传文件
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	public String saveFile(String path, MultipartFile file) {

		String oriFileName = file.getOriginalFilename();//原文件名
		if(Tools.isEmpty(oriFileName)){
			return null ;
		}
		//得到后缀
		String hz = oriFileName.substring(oriFileName.lastIndexOf("."));
		//新文件名
		String newFileName = Tools.getTimeStamp()+"_"+Tools.getRandomString(8)+""+hz;

		String targetPath = config.getDir() ; //存储的目标地址
		if(Tools.isEmpty(targetPath)){ //如果配置的文件为空
			return "未配置文件存储文件夹";
		}
		path = path+"/"+sdf.format(new Date());//加上月份
		File fileNew=new File(targetPath+"/"+path);
		if(!fileNew.exists()){
			fileNew.mkdirs();//创建新的文件夹
		}


		String dataBasePath = path+"/"+newFileName;//数据库保存地址
		targetPath +="/"+dataBasePath;

		File targetFile = new File(targetPath);
		try {
			//保存文件
			//使用此方法保存必须要绝对路径且文件夹必须已存在,否则报错
			file.transferTo(targetFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return config.getPath()+""+dataBasePath;
	}

	/**
	 * 需要保存的文件地址和文件
	 * @param path 传入的地址名称,例如,会员模块,传入的地址是member,也可以是member/2017/这样的多重文件夹
	 * @param file
	 * @return  地址字符串,注意,如果返回的是空,则说明没有选择上传文件
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	public String saveFiletwo(String path, MultipartFile file) {
		String oriFileName = file.getOriginalFilename();//原文件名
		if(Tools.isEmpty(oriFileName)){
			return null ;
		}
		//得到后缀
		String hz = oriFileName.substring(oriFileName.lastIndexOf("."));
		//新文件名
		String newFileName = Tools.getTimeStamp()+"_"+Tools.getRandomString(8)+""+hz;

		String targetPath = config.getDir() ; //存储的目标地址

		if(Tools.isEmpty(targetPath)){ //如果配置的文件为空
			return "未配置文件存储文件夹";
		}
		path = path+"/"+sdf.format(new Date());//加上月份

		File fileNew=new File(targetPath+"/"+path);
		if(!fileNew.exists()){
			fileNew.mkdirs();//创建新的文件夹
		}

		String dataBasePath = path+"/"+newFileName;//数据库保存地址
		targetPath +="/"+dataBasePath;
		//System.out.println("存储的目标地址 = " + targetPath);
		File targetFile = new File(targetPath);


		/*if(size>=7380){
			Thumbnails.of(fileSavePath).scale(1f).outputQuality(0.01f).toFile(smallFileSavePath);
			tempFile.delete();
			return getResultMap(SUCCESS_CODE,smallerPictureName);
		}else if(size>=4096&&size<7380){
			Thumbnails.of(fileSavePath).scale(1f).outputQuality(0.2f).toFile(smallFileSavePath);
			tempFile.delete();
			return getResultMap(SUCCESS_CODE,smallerPictureName);
		}else if(size>=1024&&size<4096){
			Thumbnails.of(fileSavePath).scale(1f).outputQuality(0.3f).toFile(smallFileSavePath);
			tempFile.delete();
			return getResultMap(SUCCESS_CODE,smallerPictureName);
		}else if(size>=600&&size<1024){
			Thumbnails.of(fileSavePath).scale(1f).outputQuality(1f).toFile(smallFileSavePath);
			tempFile.delete();
			return getResultMap(SUCCESS_CODE,smallerPictureName);
		}else{
			return getResultMap(SUCCESS_CODE,pictureName);
		}
	} catch (Exception e) {
		return getResultMap(ERROR_CODE,"图片上传失败，请稍后重试，或联系管理员");
	}
}*/
		try {
			//保存文件
			//使用此方法保存必须要绝对路径且文件夹必须已存在,否则报错
			file.transferTo(targetFile);//已上传到本地
			long size = file.getSize()/1024;
			//判断文件大小对图片质量进行压缩,尺寸不变，范围0.01~1.0,值越低压缩效率越高。图片低于600K不进行压缩
			if(size>=7380){
				Thumbnails.of(targetPath).scale(1f).outputQuality(0.01f).toFile(targetPath);
			}else if(size>=4096&&size<7380){
				Thumbnails.of(targetPath).scale(1f).outputQuality(0.1f).toFile(targetPath);
			}else if(size>=1024&&size<4096){
				Thumbnails.of(targetPath).scale(1f).outputQuality(0.2f).toFile(targetPath);
			}else if(size>=300&&size<1024){
				Thumbnails.of(targetPath).scale(1f).outputQuality(0.2f).toFile(targetPath);
			}else if(size<300){
				Thumbnails.of(targetPath).scale(1f).outputQuality(0.9f).toFile(targetPath);
			}
			//targetFile.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return config.getPath()+""+dataBasePath;
	}
	public void addPdf(String str,String filePath,String fileUrl){
		try {
			System.out.println(str+"啦啦啦");
			System.out.println("====="+fileUrl);
			System.out.println("==="+filePath);
			OutputStream file = new FileOutputStream(new File(fileUrl));

			Document document = new Document();
			PdfWriter.getInstance(document, file);
			document.open();

			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
			Paragraph pragraph=new Paragraph(str, FontChinese);
			document.add(new Paragraph(pragraph));
			Font FontChinese12 = new Font(bfChinese, 10, Font.NORMAL);
			Paragraph pragraph2=new Paragraph("甲方签名:", FontChinese12);
			document.add(pragraph2);
			Image jpg = Image.getInstance(filePath);
			jpg.setAlignment(Image.ALIGN_CENTER);
			jpg.scalePercent(10.8f);
			document.add(jpg);

			document.close();
			file.close();

		} catch (Exception e) {

			e.printStackTrace();
		}
	}


	public static void main(String[] args) throws IOException {
		String str="789.jkl.jpg";
		String hz = str.substring(str.lastIndexOf("."));
		System.out.println(hz);
		File tempFile = new File("D:\\upload\\555d4153d8ad7.jpg");
		Thumbnails.of("D:\\upload\\555d4153d8ad7.jpg").scale(0.25f).toFile("D:\\upload\\555d4153d8ad7 -22.jpg");
		tempFile.delete();
	}
}
